/**
 * Created by Tuffy on 16/1/18.
 */
'use strict';

angular.module('starter.controllers')

.controller('HotelListPrivateCtrl', ['$scope', '$rootScope','$ionicModal', '$state' , '$cordovaToast','$ionicLoading','$ionicPopup','CrmHttp',
    function ($scope, $rootScope,$ionicModal, $state, $cordovaToast,$ionicLoading,$ionicPopup,CrmHttp) {
        $rootScope.isPrivateFlag = true;
        $rootScope.EDITE_MODEL = true; // 私海为编辑模式
        $scope.TAB_HOTEL_INDEX = 0; // 当前tab酒店索引
        $scope.TABS_HOTEL_LIST = []; // tabs酒店列表
        $scope.tabs = [{
            id: '1',
            name: '全部',
            loaded: true,
            templateUrl: 'templates/hotel-list-private-all.html',
            controller: 'HotelListPrivateAllCtrl'
        }, {
            id: '2',
            name: '危险',
            templateUrl: 'templates/hotel-list-private-danger.html',
            controller: 'HotelListPrivateDangerCtrl'
        }, {
            id: '3',
            name: '分销未开通',
            templateUrl: 'templates/hotel-list-private-swath.html',
            controller: 'HotelListPrivateSwathCtrl'
        }, {
            id: '4',
            name: '洗涤未开通',
            templateUrl: 'templates/hotel-list-private-wash.html',
            controller: 'HotelListPrivateWashCtrl'
        }];
        $scope.onSlideMove = function (data) {
            var _this = $scope.tabs[data.index];
            $scope.TAB_HOTEL_INDEX = data.index;
            if (!_this.loaded) {
                _this.loaded = true;
            }
        };

        //电话列表
        $scope.showTelList = function ($event, hotel) {
            $rootScope.EDIT_HOTEL = hotel;
            $state.go('app.hotel-contact', {hotelId: hotel.id});
            $event.stopPropagation();
        };
        //进入酒店详细页
        $scope.goHotelMain = function (hotel) {
            //编辑模式需要的内容
            $rootScope.EDIT_HOTEL = hotel; //酒店数据
            $state.go('app.hotel-main', {hotelId: hotel.id});
        };
        // 退回公海
        $scope.returnPublic = function($event, hotel) {
            var confirmPopup = $ionicPopup.confirm({
                title: '退回公海',
                template: '您确认将【'+hotel.hotelName+'】退回公海吗？',
                cancelText:'取消',
                cancelType:'button-block button-outline button-energized-crm',
                okText: '确定',
                okType: 'button-block button-energized-crm',
            });

            confirmPopup.then(function(res) {
                if(res) {
                    $ionicLoading.show({
                        template: '退回中...'
                    });
                    CrmHttp.post('/crm/crm-hotel-public/hotel-return', {
                            hotelId: hotel.id,
                            operationUserId: Login.getLoginInfo.userId
                        })
                        .then(function(resultData) {
                            var res = resultData.data;
                            if (res.success) {
                                hotel.isReturn = true;
                                $cordovaToast.showShortBottom('退回成功');
                            } else {
                                $cordovaToast.showShortBottom(res.errorMsg);
                            }
                        }, function(error) {
                            $cordovaToast.showShortBottom('退回错误');
                        }).finally(function () {
                        $ionicLoading.hide();
                    });
                } else {
                    // console.log('You are not sure');
                }
            });
            $event.stopPropagation();
        };
}])
// 所有
.controller('HotelListPrivateAllCtrl', ['$scope', '$rootScope', '$timeout', '$cordovaActionSheet', '$ionicModal', '$state', 'HotelService', '$cordovaToast', 'ionicMaterialMotion', 'ionicMaterialInk', 'CrmHttp', '$ionicLoading',
    function ($scope, $rootScope, $timeout, $cordovaActionSheet, $ionicModal, $state, HotelService, $cordovaToast, ionicMaterialMotion, ionicMaterialInk, CrmHttp, $ionicLoading) {

        var isHealthy = $state.params.isHealthy; //{1:'健康',2:'一般',3:'危险'}

        $scope.title = '私海';
        $scope.HOTEL_TYPE = 'hotel-list-private-all';
        //筛选
        var filter = $rootScope.$on($scope.HOTEL_TYPE, function (event, data) {
            $scope.hotels.page.pageNum = 1;
            $scope.hotels.search.filter = data;
            $scope.hotels.refresh();
        });
        $scope.$on('$destroy', function () {
            filter();
            if ($rootScope.FILTER_DATE && $rootScope.FILTER_DATE[$scope.HOTEL_TYPE]) {
                delete $rootScope.FILTER_DATE[$scope.HOTEL_TYPE];
            }
            delete $rootScope.EDITE_MODEL;
            delete $scope.HOTEL_TYPE;
            delete $rootScope.EDIT_HOTEL;
        });

        $scope.isFilter = function () {
            return Object.keys($scope.hotels.search.filter).length > 0;
        };
        /**
         * hotels
         */
        $scope.hotels = {
            search: {
                value: '',
                filter: {},
                doAct: function () {
                    if (this.timer) {
                        $timeout.cancel(this.timer);
                    }
                    this.timer = $timeout(function () {
                        $scope.hotels.page.pageNum = 1;
                        $scope.hotels.data = [];
                        $scope.hotels.loadData();
                    }, 500);
                }
            },
            page: {
                pageNum: 0,
                pageSize: 10,
                total: 0
            },
            loaded: false,
            data: [],
            loadData: function (okFunc) {
                var _this = this,
                    params = {
                        hotelName: _this.search.value,
                        pageNum: _this.page.pageNum,
                        pageSize: _this.page.pageSize
                    };
                _this.loaded = false;
                for (var key in _this.search.filter) {
                    params[key] = _this.search.filter[key];
                }
                HotelService.findListPrivate(params)
                    .success(function (res) {
                        if (res.success) {
                            if(typeof (okFunc) == 'function'){
                                okFunc();
                            }
                            var _data = res.attribute.rows;
                            if (!_data || _data.length <= 0) {
                                _this.moreDataCanBeLoaded = false;
                                $cordovaToast.showShortBottom('已加载到尾页');
                                return;
                            }
                            _this.data = _this.data.concat(_data);
                            _this.page.total = res.attribute.total;
                        } else {
                            _this.page.total = 0;
                            console.log('加载失败');
                        }
                    }).error(function (error) {
                    _this.page.total = 0;
                    console.log(error);
                }).finally(function () {
                    _this.loaded = true;
                    if (_this.page.total<_this.page.pageSize) {
                        _this.moreDataCanBeLoaded = false;
                    }
                    $scope.$broadcast('scroll.refreshComplete');
                    $scope.$broadcast('scroll.infiniteScrollComplete');
                    $timeout(function () {
                        ionicMaterialMotion.blinds();
                    }, 200);

                    $timeout(function () {
                        ionicMaterialInk.displayEffect();
                    }, 1000);
                    $scope.TABS_HOTEL_LIST[$scope.TAB_HOTEL_INDEX] = {
                        count: _this.data.length,
                        total: _this.page.total
                    };
                });
            },
            moreDataCanBeLoaded: true,
            loadMore: function () {
                this.page.pageNum++;
                this.loadData();
            },
            refresh: function () {
                var _this = this;
                _this.page.pageNum = 1;
                _this.loadData(function(){
                    _this.data = [];
                });
            }
        };

        //onload
        if (isHealthy) {
            $scope.hotels.search.filter = {
                'is_healthy': isHealthy
            };
            if (!$rootScope.FILTER_DATE)
                $rootScope.FILTER_DATE = {};
            $rootScope.FILTER_DATE[$scope.HOTEL_TYPE] = {
                backData: $scope.hotels.search.filter
            };
        }
}])
// 危险
.controller('HotelListPrivateDangerCtrl', ['$scope','$rootScope', '$timeout', '$cordovaActionSheet', '$ionicModal', '$state', 'HotelService','$cordovaToast', 'ionicMaterialMotion', 'ionicMaterialInk', 'CrmHttp', '$ionicLoading',
    function($scope,$rootScope, $timeout, $cordovaActionSheet, $ionicModal, $state, HotelService,$cordovaToast, ionicMaterialMotion, ionicMaterialInk, CrmHttp, $ionicLoading) {

        var isHealthy = $state.params.isHealthy; //{1:'健康',2:'一般',3:'危险'}

        $scope.title =  '私海';
        $scope.HOTEL_TYPE = 'hotel-list-private-danger';
        //筛选
        var filter =  $rootScope.$on($scope.HOTEL_TYPE,function(event,data){
            $scope.hotels.page.pageNum = 1;
            $scope.hotels.search.filter = data;
            $scope.hotels.refresh();
        });

        $scope.$on('$destroy',function(){
            filter();
            if($rootScope.FILTER_DATE && $rootScope.FILTER_DATE[$scope.HOTEL_TYPE]){
                delete $rootScope.FILTER_DATE[$scope.HOTEL_TYPE];
            }
            delete $rootScope.EDITE_MODEL;
            delete $scope.HOTEL_TYPE;
            delete $rootScope.EDIT_HOTEL;
        });

        $scope.isFilter = function(){
            return  Object.keys($scope.hotels.search.filter).length>0;
        };

        /**
         * hotels
         */
        $scope.hotels = {
            search: {
                value: '',
                filter: {},
                doAct: function () {
                    if (this.timer) {
                        $timeout.cancel(this.timer);
                    }
                    this.timer = $timeout(function () {
                        $scope.hotels.page.pageNum = 1;
                        $scope.hotels.data = [];
                        $scope.hotels.loadData();
                    }, 500);
                }
            },
            page: {
                pageNum: 0,
                pageSize: 10,
                total: 0
            },
            loaded: false,
            data: [],
            loadData: function (okFunc) {
                var _this = this,
                    params = {
                        is_healthy:3,
                        hotelName: _this.search.value,
                        pageNum: _this.page.pageNum,
                        pageSize: _this.page.pageSize
                    };
                _this.loaded = false;
                for (var key in _this.search.filter) {
                    params[key] = _this.search.filter[key];
                }
                HotelService.findListPrivate(params)
                    .success(function (res) {
                        if (res.success) {
                            if(typeof (okFunc) == 'function'){
                                okFunc();
                            }
                            var _data = res.attribute.rows;
                            if (!_data || _data.length <= 0) {
                                _this.moreDataCanBeLoaded = false;
                                $cordovaToast.showShortBottom('已加载到尾页');
                                return;
                            }
                            _this.data = _this.data.concat(_data);
                            _this.page.total = res.attribute.total;
                        } else {
                            _this.page.total = 0;
                            console.log('加载失败');
                        }
                    }).error(function (error) {
                    _this.page.total = 0;
                    console.log(error);
                }).finally(function () {
                    _this.loaded = true;
                    if (_this.page.total<_this.page.pageSize) {
                        _this.moreDataCanBeLoaded = false;
                    }
                    $scope.$broadcast('scroll.refreshComplete');
                    $scope.$broadcast('scroll.infiniteScrollComplete');
                    $timeout(function () {
                        ionicMaterialMotion.blinds();
                    }, 200);

                    $timeout(function () {
                        ionicMaterialInk.displayEffect();
                    }, 1000);
                    $scope.TABS_HOTEL_LIST[$scope.TAB_HOTEL_INDEX] = {
                        count: _this.data.length,
                        total: _this.page.total
                    };
                });
            },
            moreDataCanBeLoaded: true,
            loadMore: function () {
                this.page.pageNum++;
                this.loadData();
            },
            refresh: function () {
                var _this = this;
                _this.page.pageNum = 1;
                _this.loadData(function(){
                    _this.data = [];
                });
            }
        };
}])
// 分销
.controller('HotelListPrivateSwathCtrl', ['$scope','$rootScope', '$timeout', '$cordovaActionSheet', '$ionicModal', '$state', 'HotelService','$cordovaToast', 'ionicMaterialMotion', 'ionicMaterialInk', 'CrmHttp', '$ionicLoading',
    function($scope,$rootScope, $timeout, $cordovaActionSheet, $ionicModal, $state, HotelService,$cordovaToast, ionicMaterialMotion, ionicMaterialInk, CrmHttp, $ionicLoading) {

        var isHealthy = $state.params.isHealthy; //{1:'健康',2:'一般',3:'危险'}

        $scope.title =  '私海';
        $scope.HOTEL_TYPE = 'hotel-list-private-swath';
        //筛选
        var filter =  $rootScope.$on($scope.HOTEL_TYPE,function(event,data){
            $scope.hotels.page.pageNum = 1;
            $scope.hotels.search.filter = data;
            $scope.hotels.refresh();
        });

        $scope.$on('$destroy',function(){
            filter();
            if($rootScope.FILTER_DATE && $rootScope.FILTER_DATE[$scope.HOTEL_TYPE]){
                delete $rootScope.FILTER_DATE[$scope.HOTEL_TYPE];
            }
            delete $rootScope.EDITE_MODEL;
            delete $scope.HOTEL_TYPE;
            delete $rootScope.EDIT_HOTEL;
        });

        $scope.isFilter = function(){
            return  Object.keys($scope.hotels.search.filter).length>0;
        };

        /**
         * hotels
         */
        $scope.hotels = {
            search: {
                value: '',
                filter: {},
                doAct: function () {
                    if (this.timer) {
                        $timeout.cancel(this.timer);
                    }
                    this.timer = $timeout(function () {
                        $scope.hotels.page.pageNum = 1;
                        $scope.hotels.data = [];
                        $scope.hotels.loadData();
                    }, 500);
                }
            },
            page: {
                pageNum: 0,
                pageSize: 10,
                total: 0
            },
            loaded: false,
            data: [],
            loadData: function (okFunc) {
                var _this = this,
                    params = {
                        sffxhz:0,
                        hotelName: _this.search.value,
                        pageNum: _this.page.pageNum,
                        pageSize: _this.page.pageSize
                    };
                _this.loaded = false;
                for (var key in _this.search.filter) {
                    params[key] = _this.search.filter[key];
                }
                HotelService.findListPrivate(params)
                    .success(function (res) {
                        if (res.success) {
                            if(typeof (okFunc) == 'function'){
                                okFunc();
                            }
                            var _data = res.attribute.rows;
                            if (!_data || _data.length <= 0) {
                                _this.moreDataCanBeLoaded = false;
                                $cordovaToast.showShortBottom('已加载到尾页');
                                return;
                            }
                            _this.data = _this.data.concat(_data);
                            _this.page.total = res.attribute.total;
                        } else {
                            _this.page.total = 0;
                            console.log('加载失败');
                        }
                    }).error(function (error) {
                    _this.page.total = 0;
                    console.log(error);
                }).finally(function () {
                    _this.loaded = true;
                    if (_this.page.total<_this.page.pageSize) {
                        _this.moreDataCanBeLoaded = false;
                    }
                    $scope.$broadcast('scroll.refreshComplete');
                    $scope.$broadcast('scroll.infiniteScrollComplete');
                    $timeout(function () {
                        ionicMaterialMotion.blinds();
                    }, 200);

                    $timeout(function () {
                        ionicMaterialInk.displayEffect();
                    }, 1000);
                    $scope.TABS_HOTEL_LIST[$scope.TAB_HOTEL_INDEX] = {
                        count: _this.data.length,
                        total: _this.page.total
                    };
                });
            },
            moreDataCanBeLoaded: true,
            loadMore: function () {
                this.page.pageNum++;
                this.loadData();
            },
            refresh: function () {
                var _this = this;
                _this.page.pageNum = 1;
                _this.loadData(function(){
                    _this.data = [];
                });
            }
        };
}])
// 洗涤
.controller('HotelListPrivateWashCtrl', ['$scope','$rootScope', '$timeout', '$cordovaActionSheet', '$ionicModal', '$state', 'HotelService','$cordovaToast', 'ionicMaterialMotion', 'ionicMaterialInk', 'CrmHttp', '$ionicLoading',
    function($scope,$rootScope, $timeout, $cordovaActionSheet, $ionicModal, $state, HotelService,$cordovaToast, ionicMaterialMotion, ionicMaterialInk, CrmHttp, $ionicLoading) {

        var isHealthy = $state.params.isHealthy; //{1:'健康',2:'一般',3:'危险'}

        $scope.title =  '私海';
        $scope.HOTEL_TYPE = 'hotel-list-private-wash';
        //筛选
        var filter =  $rootScope.$on($scope.HOTEL_TYPE,function(event,data){
            $scope.hotels.page.pageNum = 1;
            $scope.hotels.search.filter = data;
            $scope.hotels.refresh();
        });

        $scope.$on('$destroy',function(){
            filter();
            if($rootScope.FILTER_DATE && $rootScope.FILTER_DATE[$scope.HOTEL_TYPE]){
                delete $rootScope.FILTER_DATE[$scope.HOTEL_TYPE];
            }
            delete $rootScope.EDITE_MODEL;
            delete $scope.HOTEL_TYPE;
            delete $rootScope.EDIT_HOTEL;
        });

        $scope.isFilter = function(){
            return  Object.keys($scope.hotels.search.filter).length>0;
        };

        /**
         * hotels
         */
        $scope.hotels = {
            search: {
                value: '',
                filter: {},
                doAct: function () {
                    if (this.timer) {
                        $timeout.cancel(this.timer);
                    }
                    this.timer = $timeout(function () {
                        $scope.hotels.page.pageNum = 1;
                        $scope.hotels.data = [];
                        $scope.hotels.loadData();
                    }, 500);
                }
            },
            page: {
                pageNum: 0,
                pageSize: 10,
                total: 0
            },
            loaded: false,
            data: [],
            loadData: function (okFunc) {
                var _this = this,
                    params = {
                        sfxdhz:0,
                        hotelName: _this.search.value,
                        pageNum: _this.page.pageNum,
                        pageSize: _this.page.pageSize
                    };
                _this.loaded = false;
                for (var key in _this.search.filter) {
                    params[key] = _this.search.filter[key];
                }
                HotelService.findListPrivate(params)
                    .success(function (res) {
                        if (res.success) {
                            if(typeof (okFunc) == 'function'){
                                okFunc();
                            }
                            var _data = res.attribute.rows;
                            if (!_data || _data.length <= 0) {
                                _this.moreDataCanBeLoaded = false;
                                $cordovaToast.showShortBottom('已加载到尾页');
                                return;
                            }
                            _this.data = _this.data.concat(_data);
                            _this.page.total = res.attribute.total;
                        } else {
                            _this.page.total = 0;
                            console.log('加载失败');
                        }
                    }).error(function (error) {
                    _this.page.total = 0;
                    console.log(error);
                }).finally(function () {
                    _this.loaded = true;
                    if (_this.page.total<_this.page.pageSize) {
                        _this.moreDataCanBeLoaded = false;
                    }
                    $scope.$broadcast('scroll.refreshComplete');
                    $scope.$broadcast('scroll.infiniteScrollComplete');
                    $timeout(function () {
                        ionicMaterialMotion.blinds();
                    }, 200);

                    $timeout(function () {
                        ionicMaterialInk.displayEffect();
                    }, 1000);
                    $scope.TABS_HOTEL_LIST[$scope.TAB_HOTEL_INDEX] = {
                        count: _this.data.length,
                        total: _this.page.total
                    };
                });
            },
            moreDataCanBeLoaded: true,
            loadMore: function () {
                this.page.pageNum++;
                this.loadData();
            },
            refresh: function () {
                var _this = this;
                _this.page.pageNum = 1;
                _this.loadData(function(){
                    _this.data = [];
                });
            }
        };
}]);